﻿using System;
using System.Windows;
using System.Windows.Input;
using WickedFlame.Core.Shapes;

namespace WickedFlame.Core.Tools
{
    public class CurveTool : FactoryTool
    {
        public CurveTool()
            : base()
        {
        }

        protected override VectorShape CreateShape(Point mousePosition)
        {
            var shape = new CurveShape(mousePosition, mousePosition);

            shape.StrokeThickness = ShapePropertySettings.Settings.StrokeThickness;
            shape.Stroke = ShapePropertySettings.Settings.Stroke;

            return shape;
        }

        public override void OnMouseLeftButtonUp(DrawingCanvas sender, MouseButtonEventArgs e)
        {
            var shape = sender.SelectedLayer.ShapeList.TopShape as CurveShape;

            if (shape != null)
            {
                //reposition handle 2 and 3
                double distx = (shape.EndPoint.X - shape.StartPoint.X);
                double disty = (shape.EndPoint.Y - shape.StartPoint.Y);

                //-50% from start
                double dx = distx - ((distx / 100) * 50);
                double dy = disty - ((disty / 100) * 50);
                shape.CurvePoint = new Point(shape.StartPoint.X + dx, shape.StartPoint.Y + dy);

                //Add shape to history
                sender.PushHistoryCommand(new WickedFlame.Core.History.AddCommand(shape, sender.SelectedLayerIndex));

                shape.CallChangeEndHandler();
            }
        }

        public override void OnMouseMove(DrawingCanvas sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point m = Mouse.GetPosition(sender);

                var shape = sender.SelectedLayer.ShapeList.TopShape as CurveShape;
                if (shape != null)
                    shape.MoveTracker(m, 3);
            }
        }
    }
}
